package com.example.administrator.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.administrator.base.BaseActivity;
import com.example.administrator.bean.LoginStatusBean;
import com.example.administrator.module.UrlDefine;
import com.example.administrator.testapk.R;
import com.example.administrator.utils.JsonUtils;
import com.example.administrator.utils.NetUtils;
import com.example.administrator.utils.SpUtils;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

public class AtyLogin extends BaseActivity {
    @BindView(R.id.btn_ok_login)
    Button btn_ok_login;
    @BindView(R.id.tv_regi)
    TextView tv_regi;
    @BindView(R.id.tv_find_pwd)
    TextView tv_find_pwd;
    @BindView(R.id.et_phone_login)
    EditText et_phone_login;
    @BindView(R.id.et_pwd_login)
    EditText et_pwd_login;

    private Context context;

    private static final String TAG = "AtyLogin";
    private boolean isFirstLoginFlag = false;   //首次登陆标志位

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        context = this.getApplicationContext();
    }

    @Override
    protected void initView() {
        btn_ok_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(et_phone_login.getText().toString()) && !TextUtils.isEmpty(et_pwd_login.getText().toString())) {
                    showLoadBar("请稍后");

                    Map<String,String> reqbody = new ConcurrentSkipListMap<>();
                    reqbody.put("loginName", et_phone_login.getText().toString());
                    reqbody.put("password", et_pwd_login.getText().toString());

                    NetUtils.getInstance().postDataAsynToNet(UrlDefine.ACTION_URL_LOGIN, reqbody, new NetUtils.MyNetCall() {
                        @Override
                        public void success(Call call, Response response) throws IOException {
                            final String loginStatus = response.body().string();
                            Log.e("loginStatus",loginStatus);
                            final LoginStatusBean loginStatusBean = JsonUtils.JsonStr2LoginStatusBean(loginStatus);
                            Log.d(TAG,loginStatusBean.getStatus() +"");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    hideLoadBar();
                                    if(loginStatusBean.getStatus() == 200){
                                        isFirstLoginFlag = true;
                                        SpUtils.put(AtyLogin.this,"phone",et_phone_login.getText().toString());
                                        SpUtils.put(AtyLogin.this,"pwd",et_pwd_login.getText().toString());
                                        SpUtils.put(AtyLogin.this,"isFirstLoginFlag",isFirstLoginFlag);
                                        overlay(AtyMain.class);
                                    }else{
                                        toast(loginStatusBean.getMsg());
                                    }
                                }
                            });
                        }

                        @Override
                        public void failed(Call call, IOException e) {
                            Log.e(TAG,e.getMessage());
                        }
                    });
                }
            }
        });

         tv_regi.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 overlay(AtyRegi.class);
             }
         });

        tv_find_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overlay(AtyResetPwd.class);
            }
        });
    }

}



